package com.sojson.controller.po;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.GoodCateParamVerify;
import com.sojson.entity.bo.Select;
import com.sojson.entity.po.GoodCate;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.result.Result;
import com.sojson.service.GoodCateService;

import io.swagger.annotations.Api;

/**
 * 商品分类表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "商品分类表管理")
@RestController
@RequestMapping(value = "/goodCate")
public class GoodCateController {

    @Resource
    private GoodCateService goodCateServiceImpl;

    /**
     * 单条插入
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('/goodCate/insert')")
    public Object insert(@RequestBody GoodCate goodCate) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodCateParamVerify.insert(goodCate);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        goodCate.insertInit();
        goodCateServiceImpl.insert(goodCate);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody GoodCate goodCate) throws SQLException {
        return Result.success(goodCateServiceImpl.insertList(goodCate));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody GoodCate goodCate) throws SQLException {
        return Result.success(goodCateServiceImpl.deleteByDto(goodCate));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = goodCateServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<GoodCate> findAll = goodCateServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody GoodCate goodCate) throws SQLException {
        int countByDto = goodCateServiceImpl.countByDto(goodCate);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findByDto", method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('/goodCate/findByDto')")
    public Object findByDto(GoodCate goodCate) throws SQLException {
        List<GoodCate> findByDto = goodCateServiceImpl.findByDto(goodCate);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/deleteById")
    @PreAuthorize("@ss.hasPermi('/goodCate/deleteById')")
    public Object deleteById(Integer id) throws SQLException {
        if (id == null) {
            return Result.success();
        }
        goodCateServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @DeleteMapping(value = "/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids") ArrayList<Integer> ids) throws SQLException {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(goodCateServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody GoodCate goodCate) throws SQLException {
        goodCateServiceImpl.updateById(goodCate);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody GoodCate goodCate) throws SQLException {
        return Result.success(goodCateServiceImpl.updateStaticByIdList(goodCate));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody GoodCate goodCate) throws SQLException {
        return Result.success(goodCateServiceImpl.updateChangeByIdList(goodCate));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/goodCate/updatePartialById')")
    public Object updatePartialById(@RequestBody GoodCate goodCate) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodCateParamVerify.updatePartialById(goodCate);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        goodCate.updateInit();
        goodCate.setUpdateNull();
        goodCateServiceImpl.updatePartialById(goodCate);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody GoodCate goodCate) throws SQLException {
        return Result.success(goodCateServiceImpl.updateStaticPartialByIdList(goodCate));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody GoodCate goodCate) throws SQLException {
        return Result.success(goodCateServiceImpl.updateChangePartialByIdList(goodCate));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) throws SQLException {
        GoodCate findById = goodCateServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<GoodCate> findByIdList = goodCateServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody GoodCate goodCate) throws SQLException {
        ArrayList<Integer> findIdByDto = goodCateServiceImpl.findIdByDto(goodCate);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        goodCateServiceImpl.clear();
        return Result.success();
    }

    /**
     * 通过id更新状态
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updateStatusById")
    @PreAuthorize("@ss.hasPermi('/goodCate/updateStatusById')")
    public Object updateStatusById(@RequestBody GoodCate goodCate) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodCateParamVerify.updateStatusById(goodCate);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        GoodCate gdCate = new GoodCate();
        gdCate.updateInit();
        gdCate.setId(goodCate.getId());
        gdCate.setStatus(goodCate.getStatus());
        goodCateServiceImpl.updatePartialById(gdCate);
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param goodCate
     * @return
     */
    @GetMapping("/findSelect")
    public Object findSelect(GoodCate goodCate) throws SQLException {
        List<Select> findSelect = goodCateServiceImpl.findSelect(goodCate);
        return Result.success(findSelect, findSelect);
    }

    /**
     * 根据对象查询
     * 
     * @param goodCate
     * @return
     */
    @GetMapping("/selectApp")
    public Object selectApp(GoodCate goodCate) throws SQLException {
        goodCate.setStatus(EGeneralStatus.NORMAL.getCode());
        List<Select> findSelect = goodCateServiceImpl.findSelect(goodCate);
        return Result.success(findSelect, findSelect);
    }

    /**
     * 根据对象查询
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findAppByDto", method = RequestMethod.GET)
    public Object findAppByDto(GoodCate goodCate) throws SQLException {
        goodCate.setStatus(EGeneralStatus.NORMAL.getCode());
        List<GoodCate> findByDto = goodCateServiceImpl.findByDto(goodCate);
        return Result.success(findByDto, findByDto);
    }

}